package com.jz.kufangkuqu.kufang.service.impl;

import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.kufangkuqu.kufang.bean.*;
import com.jz.kufangkuqu.kufang.mapper.KuFangMapper;
import com.jz.kufangkuqu.kufang.service.KuFangService;
import com.jz.kufangkuqu.kuqu.bean.KuQu;
import com.jz.kufangkuqu.kuwei.bean.KuWei;
import com.jz.kufangkuqu.shuxing.bean.ShuXing;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KuFangServiceImpl implements KuFangService {

    @Autowired
    private KuFangMapper kuFangMapper;



    @Override
    public ResultVO queryPage(KuFangSelect kuFangSelectVo) {
        List<KuFang> kuFangList = kuFangMapper.queryPage(kuFangSelectVo);
        for (KuFang plant: kuFangList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = kuFangMapper.count(kuFangSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(kuFangSelectVo.getPageIndex());
        allRecordsVo.setPageSize(kuFangSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(kuFangList);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO queryKuFang() {
       List<KuFang> kuFangList = kuFangMapper.queryBiaoShi();

        return  ResultBuildVO.success(kuFangList);
    }

    @Override
    public ResultVO queryDiQu(Integer pid) {
        List<DiQu> diQus = kuFangMapper.queryDiQu( pid);
        return ResultBuildVO.success(diQus);
    }

    @Override
    public ResultVO add(KuFangAdd kuFangAdd) {
        Date now = new Date();
        KuFang legalPlantVo1 = new KuFang();
        BeanUtils.copyProperties(kuFangAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        kuFangMapper.add(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO update(KuFangUpdate kuFangUpdate) {
        Date now = new Date();
        KuFang legalPlant1 = new KuFang();
        BeanUtils.copyProperties(kuFangUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = kuFangMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(KuFangUpdatestatus kuFangUpdatestatus) {
        Date now = new Date();
        KuFang legalPlant1 = new KuFang();
        BeanUtils.copyProperties(kuFangUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        kuFangMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO queryKuFangXiaLa() {
        List<KuFang> kuFangList = kuFangMapper.queryKuFangXiaLa();
        return  ResultBuildVO.success(kuFangList);
    }

    @Override
    public ResultVO queryShuXing() {
        List<ShuXing> shuXings = kuFangMapper.queryShuXing();
        return  ResultBuildVO.success(shuXings);
    }

    @Override
    public ResultVO queryKuQuXiaLa(String id) {
        List<KuQu> kuQuList = kuFangMapper.queryKuQuXiaLa(id);
        return  ResultBuildVO.success(kuQuList);
    }

    @Override
    public ResultVO queryKuWeiXiaLa(KuWei kuWei) {
        List<KuWei> kuWeiList = kuFangMapper.queryKuWeiXiaLa(kuWei);
        return  ResultBuildVO.success(kuWeiList);
    }


}
